package indi.kider.server.employee.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Enterprise;
import indi.kider.server.employee.service.EnterpriseService;
import indi.kider.server.employee.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by shixj on 2018/4/26.
 */
@RestController
@RequestMapping("enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private AuthcService authcService;

    /**
     * @api {GET} /{id}
     * @apiGroup enterprise
     * @apiDescription 根据id获取企业信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}}
     * */
    @GetMapping("{id}")
    public CommonResponse<Enterprise> getEnterpriseById(@PathVariable Long id) {
        return CommonResponse.buildGetSuccessResponse(enterpriseService.getEnterpriseById(id));
    }
    /**
     * @api {POST} /list
     * @apiGroup enterprise
     * @apiDescription 根据字段查询获取企业信息列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise/list
     * {"num":"0","size":"8","query":{"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}],"total":1,"size":0}}
     * */
    @PostMapping("/list")
    public CommonResponse getEnterprisePages(@RequestBody PageRequest<Enterprise> request) {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(enterpriseService.getEnterprisePages(request,employee));
    }
    /**
     * @api {POST} /list/all
     * @apiGroup enterprise
     * @apiDescription 根据字段查询获取企业信息列表（不分页）====包含合作企业列表
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise/list/all
     * {"enterpriseName":"","apartmentBrand":"小机器","apartmentType":"公寓"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}]}
     * */
    @PostMapping("/list/all")
    public CommonResponse getEnterprises(@RequestBody Enterprise enterprise) {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(enterpriseService.getEnterprises(enterprise,employee));
    }

    /**
     * @api {POST} /getEnterprisesByCommunityId
     * @apiGroup enterprise
     * @apiDescription 根据公寓id获取 合作企业列表
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise/getEnterprisesByCommunityId
     * {"enterpriseName":"","apartmentBrand":"小机器","apartmentType":"公寓"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}]}
     * */
    @GetMapping("/getEnterprisesByCommunityId/{communityId}")
    public CommonResponse getEnterprisesByCommunityId(@PathVariable Long communityId) {
        try {
            Community community = new Community();
            Employee employee = authcService.getCurrentUser().getData();
            community.setId(communityId);
            if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){
                community.setEnterpriseId(employee.getEnterpriseId());
            }
            return CommonResponse.buildGetSuccessResponse(enterpriseService.getEnterprisesByCommunityId(community));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }


    /**
     * @api {POST} /getEnterprisesByHouseId
     * @apiGroup enterprise
     * @apiDescription 根据房间ID获取 合作企业
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise/getEnterprisesByCommunityId
     * {"enterpriseName":"","apartmentBrand":"小机器","apartmentType":"公寓"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}]}
     * */
    @GetMapping("/getEnterprisesByHouseId/{houseId}")
    public CommonResponse getEnterprisesByHouseId(@PathVariable Long houseId) {
        try {
            return CommonResponse.buildGetSuccessResponse(enterpriseService.getEnterprisesByHouseId(houseId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} /
     * @apiGroup enterprise
     * @apiDescription 新增企业
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise
     * {"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Enterprise enterprise, HttpServletRequest request) {
        Employee employee = authcService.getCurrentUser().getData();
        if(employee.getRoles().equals(EmployeeRoleCons.SUPERENTERPRISE)){
            enterprise.setEnId(employee.getEnterpriseId());
        }
        return CommonResponse.buildAddSuccessResponse(enterpriseService.add(enterprise));
    }
    /**
     * @api {PUT} /
     * @apiGroup enterprise
     * @apiDescription 修改企业信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise
     * {"id":1,"enterpriseName":"威星1","apartmentBrand":"小机器1","apartmentType":"公寓1"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":{"id":1,"enterpriseName":"威星1","apartmentBrand":"小机器1","apartmentType":"公寓1"}}
     */
    @PutMapping("")
    public CommonResponse modify(@RequestBody Enterprise enterprise) {
        return CommonResponse.buildUpdateSuccessResponse(enterpriseService.modify(enterprise));
    }
    /**
     * @api {DELETE} /{id}
     * @apiGroup enterprise
     * @apiDescription 删除企业
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/enterprise/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @DeleteMapping("/{id}")
    public CommonResponse delById(@PathVariable Long id, HttpServletRequest request) {
        return enterpriseService.delById(id);
    }
}
